package com.bailian.snails.consumer;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.TopicPartition;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author gaoxu
 * date: Created on 2021/11/18 下午1:52
 */
public class KafkaConsumerAnalysis {
    private static final Logger log = LoggerFactory.getLogger(KafkaConsumerAnalysis.class);

    public static final String brokerList = "localhost:9092";
    public static final String topic = "topic-demo";
    public static final String groupId = "group.demo";
    public static final AtomicBoolean isRunning = new AtomicBoolean(true);

    public static Properties initConfig() {
        Properties props = new Properties();
        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("bootstrap.servers", brokerList);
        props.put("group.id", groupId);
        props.put("client.id", "consumer.client.id.demo");
        return props;
    }

    public static void main(String[] args) {
        Properties props = initConfig();
        KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
        consumer.subscribe(Arrays.asList(topic));

        try {
            while (isRunning.get()) {
                ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(1000));

                // 一条一条消费
                for (ConsumerRecord<String, String> record : records) {
                    System.out.println("topic = " + record.topic() + ", partition = " + record.partition() + ", offset = " + record.offset());
                    System.out.println("key = " + record.key() + ", value = " + record.value());
                    //do something to process record.
                }

                // 按照分区维度进行消费(一个消费组里的消费者有可能是消费多个分区,一个分区只能一个消费者消费,反之一个消费者可能消费多个分区)
                Set<TopicPartition> partitions = records.partitions();
                for (TopicPartition partition : partitions) {
                    List<ConsumerRecord<String, String>> records1 = records.records(partition);
                    for (ConsumerRecord consumerRecord : records1) {
                        //TODO 循环处理的
                    }
                }
                // 按照主题的维度消费
                List<String> topicList = Arrays.asList("topic1", "topic2");
                ConsumerRecords<String, String> recordsByTopic = consumer.poll(Duration.ofMillis(1000));
                for (String topic : topicList) {
                    for (ConsumerRecord<String, String> record : recordsByTopic.records(topic)) {
                        System.out.println(record.topic() + " : " + record.value());
                    }
                }

            }
        } catch (Exception e) {
            log.error("occur exception ", e);
        } finally {
            consumer.close();
        }
    }
}
